using System.IO;
using System.Xml;
using UnityEngine;
using UnityEngine.UI;

public class SavesList : MonoBehaviour
{
	private string folder;

	public Button button;

	public Transform parentButton;

	public GameObject SaveInfo;

	private void Start()
	{
		if (Application.platform == RuntimePlatform.Android)
		{
			AndroidJavaClass androidJavaClass = new AndroidJavaClass("android.os.Environment");
			using (AndroidJavaObject androidJavaObject = androidJavaClass.CallStatic<AndroidJavaObject>("getExternalStorageDirectory", new object[0]))
			{
				string text = androidJavaObject.Call<string>("getPath", new object[0]);
				folder = text + "/SimpleSandbox/Saves/";
			}
		}
		else
		{
			folder = "Assets/SavePc/";
			if (!Directory.Exists(folder))
			{
				Directory.CreateDirectory(folder);
			}
		}
		GetListSaves();
	}

	public void GetListSaves()
	{
		if (!Directory.Exists(folder))
		{
			Directory.CreateDirectory(folder);
		}
		DirectoryInfo directoryInfo = new DirectoryInfo(folder);
		FileInfo[] files = directoryInfo.GetFiles("*.xml");
		FileInfo[] array = files;
		foreach (FileInfo fileInfo in array)
		{
			GameObject gameObject = Object.Instantiate(SaveInfo);
			LoadInfo component = gameObject.GetComponent<LoadInfo>();
			component.name_Load.text = ReadStringWithoutPoint(fileInfo.Name);
			gameObject.name = ReadStringWithoutPoint(fileInfo.Name);
			gameObject.transform.SetParent(parentButton);
			gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
			gameObject.transform.localPosition = new Vector3(0f, 0f, 0f);
			XmlTextReader xmlReader = new XmlTextReader(folder + fileInfo.Name);
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(xmlReader);
			XmlNode documentElement = xmlDocument.DocumentElement;
			component.saveList = this;
		}
	}

	public void DeleteFile(string filename)
	{
		string path = folder + "/" + filename;
		if (File.Exists(path))
		{
			File.Delete(path);
		}
	}

	private string ReadStringWithoutPoint(string str)
	{
		string text = string.Empty;
		for (int i = 0; str[i] != '.'; i++)
		{
			text += str[i];
		}
		return text;
	}
}
